/* 
 * Copyright 2009 The Tapestry-GAE team.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.tapestry.gae.jpa.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.apache.tapestry5.ioc.services.TypeCoercer;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTemplate;

import com.googlecode.tapestry.gae.jpa.EntityService;
import com.googlecode.tapestry.gae.model.PaginationList;
import com.googlecode.tapestry.gae.model.PaginationOptions;

/**
 * provider access JPA.
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @since 0.1
 */
@SuppressWarnings("unchecked")
public class JpaEntityServiceImpl  implements EntityService{

	private JpaTemplate jpaTemplate;
	private PaginatedJpaEntityService paginatedJpaEntityService;
	
	

	public JpaEntityServiceImpl(JpaTemplate jpaTemplate,final TypeCoercer typeCoercer){
		this.jpaTemplate = jpaTemplate;
		paginatedJpaEntityService = new PaginatedJpaEntityService(jpaTemplate,typeCoercer);
	}
	 public PaginationList paginate(final Class<?> persistClass,final Object conditions,
             final String order,final PaginationOptions options){
		 return paginatedJpaEntityService.paginate(persistClass, conditions, order, options);
		 
	 }
	public long count(Class<?> persistClass, Object conditions) {
		return paginatedJpaEntityService.count(persistClass, conditions);
	}

	public Iterator find(Class<?> persistClass, Object conditions, String order) {
		return paginatedJpaEntityService.find(persistClass, conditions, order);
	}

	public Iterator find(Class<?> persistClass, Object conditions,
			String order, int start, int offset) {
		return paginatedJpaEntityService.find(persistClass, conditions, order,
				start, offset);
	}

	
	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#execute(org.springframework.orm.jpa.JpaCallback, boolean)
	 */
	public Object execute(JpaCallback action, boolean exposeNativeEntityManager)
			throws DataAccessException {
		return this.jpaTemplate.execute(action, exposeNativeEntityManager);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#execute(org.springframework.orm.jpa.JpaCallback)
	 */
	public Object execute(JpaCallback action) throws DataAccessException {
		return this.jpaTemplate.execute(action);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#executeFind(org.springframework.orm.jpa.JpaCallback)
	 */
	public List executeFind(JpaCallback action) throws DataAccessException {
		return this.jpaTemplate.executeFind(action);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#find(java.lang.Class, java.lang.Object)
	 */
	public <T> T find(Class<T> entityClass, Object id)
			throws DataAccessException {
		return this.jpaTemplate.find(entityClass, id);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#find(java.lang.String, java.lang.Object)
	 */
	public List find(String queryString, Object... values)
			throws DataAccessException {
		return this.jpaTemplate.find(queryString, values);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#find(java.lang.String)
	 */
	public List find(String queryString) throws DataAccessException {
		return this.jpaTemplate.find(queryString);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#findByNamedParams(java.lang.String, java.util.Map)
	 */
	public List findByNamedParams(String queryString, Map<String, ?> params)
			throws DataAccessException {
		return this.jpaTemplate.findByNamedParams(queryString, params);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#findByNamedQuery(java.lang.String, java.lang.Object)
	 */
	public List findByNamedQuery(String queryName, Object... values)
			throws DataAccessException {
		return this.jpaTemplate.findByNamedQuery(queryName, values);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#findByNamedQuery(java.lang.String)
	 */
	public List findByNamedQuery(String queryName) throws DataAccessException {
		return this.jpaTemplate.findByNamedQuery(queryName);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#findByNamedQueryAndNamedParams(java.lang.String, java.util.Map)
	 */
	public List findByNamedQueryAndNamedParams(String queryName,
			Map<String, ?> params) throws DataAccessException {
		return this.jpaTemplate.findByNamedQueryAndNamedParams(queryName, params);
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#flush()
	 */
	public void flush() throws DataAccessException {
		this.jpaTemplate.flush();
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#getEntityManager()
	 */
	public EntityManager getEntityManager() {
		return this.jpaTemplate.getEntityManager();
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#getEntityManagerFactory()
	 */
	public EntityManagerFactory getEntityManagerFactory() {
		return this.jpaTemplate.getEntityManagerFactory();
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#getJpaDialect()
	 */
	public JpaDialect getJpaDialect() {
		return this.jpaTemplate.getJpaDialect();
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#getJpaPropertyMap()
	 */
	public Map getJpaPropertyMap() {
		return this.jpaTemplate.getJpaPropertyMap();
	}

	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#getReference(java.lang.Class, java.lang.Object)
	 */
	public <T> T getReference(Class<T> entityClass, Object id)
			throws DataAccessException {
		return this.jpaTemplate.getReference(entityClass, id);
	}

	public int hashCode() {
		return this.jpaTemplate.hashCode();
	}
	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#merge(T)
	 */
	public <T> T merge(T entity) throws DataAccessException {
		return this.jpaTemplate.merge(entity);
	}
	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#persist(java.lang.Object)
	 */
	public void persist(Object entity) throws DataAccessException {
		this.jpaTemplate.persist(entity);
	}
	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#refresh(java.lang.Object)
	 */
	public void refresh(Object entity) throws DataAccessException {
		this.jpaTemplate.refresh(entity);
	}
	/**
	 * @see lichen.lichen.common.services.jpa.EntityService#remove(java.lang.Object)
	 */
	public void remove(Object entity) throws DataAccessException {
		this.jpaTemplate.remove(entity);
	}
}
